﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NDde.Client;

namespace Firegraph
{
    /// <summary>
    /// Firefox is the encapsulation object representing the Firefox
    /// browser. It's primary use is to get the URL that's currently 
    /// being browsed.
    /// </summary>
    public class Firefox
    {
        /// <summary>
        /// The DDE Client which will handle communicaiton to Firefox. DDEClient
        /// is supplied by reference though NDde.Client, an external library from
        /// http://ndde.codeplex.com/
        /// </summary>
        private DdeClient oDDEClient;

        /// <summary>
        /// Constructor.
        /// </summary>
        public Firefox()
        {
            oDDEClient = new DdeClient("Firefox", "WWW_GetWindowInfo");
            oDDEClient.Connect();
        }

        /// <summary>
        /// Gets the URL request from Firefox and parses the string for the
        /// URL only, ignoring the tab title.
        /// </summary>
        /// <returns>Returns the URL from the Firefox DDE client</returns>
        public string GetCurrentURL()
        {
            string requested_url = oDDEClient.Request("URL", int.MaxValue);
            string processed_url = (string)requested_url.Split(',')[0];
            processed_url = processed_url.Substring(1, processed_url.Length - 2);
            return processed_url;
        }

        /// <summary>
        /// Shuts the client communucation down.
        /// </summary>
        public void DisconnectClient()
        {
            oDDEClient.Disconnect();
        }
    }
}
